package com.leetcode.LC_MED;

public class LC543_2 {
    int maxLen = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root != null) {
            dfs(root);
        }
        return maxLen;
    }

    public int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftNum = root.left == null ? 0 : 1 + dfs(root.left);
        int rightNum = root.right == null ? 0 : 1 + dfs(root.right);
        maxLen = Math.max(leftNum + rightNum, maxLen);
        return Math.max(leftNum, rightNum);
    }
}
